package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.LoginMapper;
import com.gxa.mapper.shezhimapper.QuanXianMapper;
import com.gxa.mapper.shezhimapper.UserMapper;
import com.gxa.pojo.User;
import com.gxa.service.shezhiservice.LoginService;
import com.gxa.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author weishen
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    QuanXianMapper quanXianMapper;

    @Override
    public R login(String username, String password) {
        R r = new R();
        r.setCode(500);
        r.setMsg("账户被冻结");
        User user = userMapper.selectUser(username);
        System.out.println("用户登录权限："+user.getQuanXianLogin());
        if(user.getQuanXianLogin()==1)
        {
            r.setCode(500);
            r.setMsg("账号或者密码错误");
            Subject subject = SecurityUtils.getSubject();
            if(!subject.isAuthenticated())
            {
                UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
                subject.login(usernamePasswordToken);
            }
            String token = JwtUtils.createJwt(user.getId()+"",JwtUtils.JWT_SECERT,user.getUsername(),JwtUtils.TTMILLIS);
            r.setToken(token);
            r.setCode(200);
            r.setMsg("登录成功");
        }
        return r;
    }
}
